﻿using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using ProjectManageSystemApi.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Utilities
{
    public static class ApiUtilities
    {
        /// <summary>
        /// 获取枚举的描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription<T>(this T value) where T : Enum
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return string.Empty;
        }

        public static string GetString<T>(this T value) where T : Enum
        {
            return value.ToString().ToLower();
        }
        public static bool NumberGtZero(this int val)
        {
            return val > 0;
        }
        public static bool NumberGtZero(this uint val)
        {
            return val > 0;
        }
        public static bool NumberGtZero(this long val)
        {
            return val > 0;
        }
        public static bool NumberGtZero(this ulong val)
        {
            return val > 0;
        }

        /// <summary>
        /// 进行Md5加密
        /// </summary>
        public static string MD5Encrypt(this string strText)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = Encoding.UTF8.GetBytes(strText);
            byte[] targetData = md5.ComputeHash(fromData);
            StringBuilder byte2String = new StringBuilder();

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String.Append(targetData[i].ToString("x2"));
            }
            return byte2String.ToString();
        }

        /// <summary>
        /// 判断是否为空
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrWhiteSpace(value);
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string GetToken(this HttpRequest request)
        {
            return request.Headers["Authorization"].ToString().Replace("Bearer ", "").Replace("bearer ", "").Replace("BEARER ", "");
        }
        public static string ReplaceBearer(this string token)
        {
            if (token.IsNullOrEmpty())
            {
                return string.Empty;
            }
            return token.ReplaceStr("Bearer ", "", true);
        }

        public static string ReplaceStr(this string str, string key, string value, bool ignoreCase)
        {
            if (string.IsNullOrWhiteSpace(str) || string.IsNullOrWhiteSpace(key))
            {
                return str;
            }
            string newstr = str.Replace(key, value);

            int i = newstr.IndexOf(key, StringComparison.OrdinalIgnoreCase);

            if (i > 0 && ignoreCase)
            {
                key = newstr.Substring(i, key.Length);
                return ReplaceStr(newstr, key, value, ignoreCase);
            }
            else
            {
                return newstr;
            }

        }
        public static PageBo<T> Default<T>(this PageBo<T> pageBo, int defPage = 1, int defSize = 10) where T : new()
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<T>();
            }
            if (pageBo.Page <= 0)
            {
                pageBo.Page = defPage;
            }
            if (pageBo.Size <= 0)
            {
                pageBo.Size = defSize;
            }
            if (pageBo.Param == null)
            {
                pageBo.Param = new T();
            }
            return pageBo;
        }
        public static string GetValue(this ICell cell)
        {
            if (cell == null)
            {
                return string.Empty;
            }
            try
            {
                switch (cell.CellType)
                {
                    case CellType.Numeric:
                    case CellType.Formula:
                        return Math.Round(cell.NumericCellValue, 2).ToString();
                    case CellType.Boolean:
                        return cell.BooleanCellValue ? "1" : "0";
                    case CellType.String:
                    case CellType.Blank:
                    case CellType.Unknown:
                    default:
                        return cell.StringCellValue?.Trim();
                }
            }
            catch
            {
                return string.Empty;
            } 
        }

        public static List<T> Copy<T>(this T[] source) 
        {
            if (source == null || source.Length < 0) 
            {
                return new List<T>();
            }
            var resultDatas = new T[source.Length];
            Array.Copy(source, 0, resultDatas, 0, source.Length);
            return resultDatas.ToList();
        }

        /// <summary>
        /// 获取毫秒时间戳(当前时间)
        /// </summary>
        /// <returns></returns>
        public static long GetMillUtcTimeStamp(this DateTime dateTime)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }
    }
}
